package com.example.supernote.ui.components

import android.net.Uri
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Preview
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.example.supernote.R
import com.example.supernote.ui.note.components.ShowMedia
import com.example.supernote.ui.note.utils.getFinalTemplate
import com.example.supernote.ui.note.utils.isErrorMsg
import com.example.supernote.ui.utils.MarkdownText
import com.example.supernote.ui.utils.getGenerateButtons

/**
 * 统一的预览界面样式：body 部分
 *
 * @param title 标题
 * @param html 内容
 * @param sourceUri 图片的Uri（当 html 为空时展示）
 */
@Composable
fun UniformPreviewBody(
    title: String = "",
    html: String? = null,
    sourceUri: Uri? = null,
    method: String = "image"
) {
    Column(
        modifier = Modifier
            .border(1.dp, MaterialTheme.colorScheme.onBackground)
            .padding(5.dp)
            .fillMaxWidth()
    ) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            UniformIcon(imageVector = Icons.Filled.Preview)
            Text(
                text = title,
                modifier = Modifier
                    .padding(5.dp),
                style = MaterialTheme.typography.labelSmall,
                color = MaterialTheme.colorScheme.onBackground
            )
        }

        Column(
            modifier = Modifier
                .heightIn(max = 400.dp)
                .verticalScroll(rememberScrollState())
        ) {
            html?.let {
                MarkdownText(html)
            }?:run {
                if (method == "image") {
                    AsyncImage(
                        model = sourceUri,
                        contentDescription = null,
                        modifier = Modifier.size(350.dp),
                        contentScale = ContentScale.Inside
                    )
                } else {
                    ShowMedia(sourceUri!!)
                }
            }
        }
    }
}

/**
 * 统一的预览界面样式：footer 部分
 *
 * @param template ai生成的模板，这里用来判断显示不同的按钮状态
 * @param isGenerated 是否正在生成模板
 * @param aiMode 当前ai的功能 ["提取", "润色", "生成", "识别", "翻译"]
 * @param confirmButtonText 确认按钮上显示的文字，默认是 "确认"
 * @param onCancel 取消按钮的回调函数
 * @param onConfirm 确认按钮的回调函数
 * @param onGenerate 生成按钮的回调函数
 */
@Composable
fun UniformPreviewFooter(
    template: String,
    isGenerated: Boolean,
    aiMode: String,
    confirmButtonText: String = stringResource(R.string.note_confirm),
    onCancel: () -> Unit,
    onConfirm: () -> Unit,
    onGenerate: () -> Unit
) {
    Row {
        val showConfirmButton = template.isNotEmpty() && !isErrorMsg(getFinalTemplate(template))
        val buttons = getGenerateButtons(aiMode)
        if (!showConfirmButton)
            Spacer(modifier = Modifier.weight(1f))
        UniformButton(
            enabled = !isGenerated,
            onClick = onGenerate,
            text = if (isGenerated) buttons[1] else if (template.isEmpty()) buttons[0] else buttons[2]
        )
        if (showConfirmButton) {
            Spacer(modifier = Modifier.weight(1f))
            UniformButton(onClick = onConfirm, text = confirmButtonText)
        }
        Spacer(modifier = Modifier.width(5.dp))
        UniformButton(
            enabled = !isGenerated,
            onClick = onCancel,
            text = stringResource(R.string.note_cancel)
        )
    }
}